package guoanmaker.provider.business.service;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import guoanmaker.provider.business.model.PersonalInvestorDisplay;
import guoanmaker.provider.business.repository.PersonalInvestorDisplayServiceRepository;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class PersonalInvestorDisplayService implements
		PersonalInvestorDisplayServiceinterface {
	
	@Autowired
	private PersonalInvestorDisplayServiceRepository personalInvestorDisplayServiceRepository;
	
	@PersistenceContext
	private EntityManager manager;
	
	@Override
	@Transactional
	public void insertPersonalPlatform(Workbook wb) {
		Sheet sheet = wb.getSheetAt(0);
		int totcount = sheet.getLastRowNum();
		for(int i=1;i<totcount;i++){
			Row row = sheet.getRow(i + 1);
			if(i==1){
			}
			if(row.getCell(0)!=null){
		          row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
		     }
			if(row.getCell(1)!=null){
				row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
			}
			if(row.getCell(2)!=null){
				row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
			}
			if(row.getCell(3)!=null){
				row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
			}
			PersonalInvestorDisplay  personalInvestorDisplay=new PersonalInvestorDisplay();
			
			if(row.getCell(0)!=null){
				personalInvestorDisplay.setName(row.getCell(0).getStringCellValue());
			}
			if(row.getCell(1)!=null){
				personalInvestorDisplay.setCorporateName(row.getCell(1).getStringCellValue());
			}
			if(row.getCell(2)!=null){
				
				personalInvestorDisplay.setBusiness(row.getCell(2).getStringCellValue());
			}
			if(row.getCell(3)!=null){
				personalInvestorDisplay.setIntroduce(row.getCell(3).getStringCellValue());
			}
			personalInvestorDisplayServiceRepository.save(personalInvestorDisplay);
			
			
		}

	}

	@Override
	public Page<PersonalInvestorDisplay> selectPersonalInvestorDisplay(
			Integer page, Integer size) {
		
		Pageable pageable  = new PageRequest(page,size);
		Page<PersonalInvestorDisplay> personalPlatform = personalInvestorDisplayServiceRepository.findAll(pageable);
		return personalPlatform;
	}

	@Override
	@Transactional
	public PersonalInvestorDisplay addorUpdatePersonalInvestorDisplay(
			PersonalInvestorDisplay personalInvestorDisplay) {
		
		return personalInvestorDisplayServiceRepository.save(personalInvestorDisplay);
	}

	@Override
	@Transactional
	public void deletePersonalInvestorDisplay(String id) {
		String jpql = "delete from PersonalInvestorDisplay p where p.id= :id"; 
		Query query = manager.createQuery(jpql).setParameter("id", id);
		int reuslt=query.executeUpdate();
		System.out.println(reuslt);
		
	}

	@Override
	public PersonalInvestorDisplay selectOnePersonalInvestorDisplay(String id) {
		
		return personalInvestorDisplayServiceRepository.findOne(id);
	}



}
